import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori,association_rules

df = pd.read_excel('./411/餐厅数据.xlsx')
print(df.head)

trsations = df['菜品'].str.split(',').to_list

te = TransactionEncoder()
te_ary = te.fit(trsations).transform(trsations)
de_enecoded = pd.DataFrame()

frequent_itemsets = apriori(de_enecoded,min_support=0.1,use_colnames=True)
frequent_itemsets.sort_values(by='support', ascending=False, inplace=True)

# 生成关联规则
rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.1)
rules = rules.sort_values(by=['lift'], ascending=False)
# frequent_itemsets.to_pickle("frequent_itemsets.pkl")
rules.to_pickle("rules.pkl")